package com.sinoglobal.ningxia.activity.vip;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.ShareAndPushVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktAsyncTask;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.widget.ToggleSlipButton;
import com.sinoglobal.ningxia.widget.ToggleSlipButton.OnChangedListener;

/**
 * 
 * @title: SynchronousShareActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description: 同步分享页面，点赞时的同步共享暂时不需要
 * @author lms
 * @date 2014-6-23 下午4:45:26
 */
public class SynchronousShare extends BaseActivity {
	private ToggleSlipButton collect, comment;// 滑动按钮
	private boolean isCollectChecked = false;// 收藏状态是否开启
	private boolean isCommentChecked = false;// 评论状态是否开启
	// private boolean isPraiseChecked = false;//点赞状态是否开启（点赞暂时不加）
	private int collectChecked;// 记录收藏时共享按钮的状态值
	private int commentChecked;// 记录评论时共享按钮的状态值
	// private int praiseChecked;// 记录点赞时共享按钮的状态值（点赞暂时不加）

	private Dialog dialog;// 对话框

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vip_synchronous_share);

		openChenJInShi();//开启沉浸式
		init();
		getValFromNetwork(MContants.EMPTY_STRING);// 获取共享的状态
		setChangeListener();
	}

	// 发送三个状态值：收藏时同步，评论时同步，点赞时评论
	private void sendSlipButtonVal() {
		String val = collectChecked + "," + commentChecked + "," + 0;
		getValFromNetwork(val);
	}

	// 滑动按钮状态改变时设置监听 1为开，0为关
	private void setChangeListener() {
		// 收藏
		collect.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				if (CheckState) {
					ItktLog.i("===sy=====checkstate=======" + CheckState + 0);
					collectChecked = 0;
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CollectCheck", true);
				} else {
					collectChecked = 1;
					ItktLog.i("===sy=====checkstate=======" + CheckState + 1);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CollectCheck", false);
					if (MContants.EMPTY_STRING.equals(SharedPreferenceUtil.getString(FlyApplication.context, "isCollectClickFist"))) {
						showConfirmDialog(0);// 开启时提示用户将通过授权的第三方收藏
						SharedPreferenceUtil.saveString(FlyApplication.context, "isCollectClickFist", "no");// 是否是进入应用后第一次点击按钮
					}
				}
			}
		});
		
		//评论
		comment.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				if (CheckState) {
					commentChecked = 0;
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CommentCheck", true);
				} else {
					commentChecked = 1;
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CommentCheck", false);
					if (MContants.EMPTY_STRING.equals(SharedPreferenceUtil.getString(FlyApplication.context, "isCommentClickFist"))) {
						showConfirmDialog(1);// 开启时提示用户将通过授权的第三方评论
						SharedPreferenceUtil.saveString(FlyApplication.context, "isCommentClickFist", "no");// 是否是进入应用后第一次点击按钮
					}
				}
			}
		});
	}

	// 设置滑动按钮状态
	private void setSlipButtonState() {
		ItktLog.i("---->" + isCollectChecked + "--" + isCommentChecked + "--");
		collect.setCheck(isCollectChecked);
		comment.setCheck(isCommentChecked);
		// praise.setCheck(isPraiseChecked);
	}

	private void init() {
		titleView.setText(R.string.synchronous_share);
		collect = (ToggleSlipButton) findViewById(R.id.collect_slip_btn);
		comment = (ToggleSlipButton) findViewById(R.id.comment_slip_btn);
		// praise = (ToggleSlipButton) findViewById(R.id.praise_slip_btn);
	}

	@Override
	protected void onResume() {
		super.onResume();
		isCollectChecked = SharedPreferenceUtil.getBoolean(FlyApplication.context, "CollectCheck");
		isCommentChecked = SharedPreferenceUtil.getBoolean(FlyApplication.context, "CommentCheck");
		// isPraiseChecked =
		// SharedPreferenceUtil.getBoolean(FlyApplication.context,
		// "PraiseCheck");

		// 记录收藏、评论、点赞时同步共享的状态值，true对应着0，false对应着1
		if (isCollectChecked) {
			collectChecked = 0;
		} else {
			collectChecked = 1;
		}
		if (isCommentChecked) {
			commentChecked = 0;
		} else {
			commentChecked = 1;
		}
		// if (isPraiseChecked) {
		// praiseChecked = 0;
		// } else {
		// praiseChecked = 1;
		// }
		// 初始化滑动按钮
		setSlipButtonState();
	}

	// 得到同步分享的状态
	private void getValFromNetwork(final String val) {
		new ItktAsyncTask<Void, Void, ShareAndPushVo>() {

			@Override
			public ShareAndPushVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getOrSendVal(FlyApplication.userId, 1, val);
			}

			@Override
			public void after(ShareAndPushVo result) {
				if (result != null) {
					if (Code.CODE_SUCCESS != result.getCode()) {
						// showShortToastMessage(result.getMessage());
						return;
					}
					ItktLog.i("---->" + result.getVal());
					// 得到返回结果的字符串值，拆分字符串，通过判断字符串中单个字符的值，确定同步共享的状态： 0为关，1为开
					String[] valArray = result.getVal().trim().split(",");
					ItktLog.e("===>>" + valArray[0] + valArray[1] + valArray[2]);
					if (0 == Integer.parseInt(valArray[0])) {
						isCollectChecked = true;
						SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CollectCheck", true);
					} else {
						isCollectChecked = false;
						SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CollectCheck", false);
					}
					if (0 == Integer.parseInt(valArray[1])) {
						isCommentChecked = true;
						SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CommentCheck", true);
					} else {
						isCommentChecked = false;
						SharedPreferenceUtil.saveBoolean(FlyApplication.context, "CommentCheck", false);
					}
					// if (0 == Integer.parseInt(valArray[2])) {
					// isPraiseChecked = true;
					// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
					// "PraiseCheck", true);
					// } else {
					// isPraiseChecked = false;
					// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
					// "PraiseCheck", false);
					// }
					setSlipButtonState();// 设置滑动按钮的状态
				}
			}

			@Override
			public void exception() {

			}

		}.execute();
	}

	@Override
	protected void onPause() {
		super.onPause();
		sendSlipButtonVal();// 更新同步共享的状态
	}

	// 创建确认对话框
	public void showConfirmDialog(int type) {
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.about_version_dialog, null);
		Button confirm = (Button) view.findViewById(R.id.confirm_btn);
		TextView alertStr = (TextView) view.findViewById(R.id.dialog_message);
		// 开启时添加弹窗提示
		switch (type) {
		case 0:
			alertStr.setText(getResources().getString(R.string.collect_synchronous_prompt));
			break;
			
		case 1:
			alertStr.setText(getResources().getString(R.string.comment_synchronous_prompt));
			break;
			
		case 2:
			alertStr.setText(getResources().getString(R.string.praise_synchronous_prompt));
			break;
		}
		builder.setView(view);
		builder.setCancelable(true);
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();
		dialog.show();
	}
}
